<template>
  <div class="com-container">
    <div class="com-chart" ref="tLine_ref"></div>
  </div>
</template>

<script>
import indexdata from '@/api/data/indexdata'

export default {
  data () {
    return {
      chartInstance: null,//图标实例
      allData: null,//图表数据
      size: null,//大小
      startYear: 0,
      endYear: 0,
    }
  },
  mounted () {
    this.initChart()
  },
  destroyed () {
    window.removeEventListener('resize', this.screenAdapter)
  },
  methods: {
    async initChart () {
      const yearData = []
      // 获取年份区间
      const res = await indexdata.getMinMaxYear()
      const startYear = res.data.minMaxYear.minYear
      const endYear = res.data.minMaxYear.maxYear
      for (let i = startYear; i <= endYear; i++) {
        yearData.push(i.toString())
      }
      this.chartInstance = this.$echarts.init(this.$refs.tLine_ref, 'dark')
      const initOption = {
        grid: {
          bottom: 70
        },
        dataZoom: [
          {
            type: 'slider',
            show: true,
            realtime: false,
            start: 0,
            end: 100,
            fillerColor: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
              //百分之0状态之下颜色的值
              {
                offset: 0,
                color: 'orange'
              },
              //百分之100状态之下颜色的值
              {
                offset: 1,
                color: 'green'
              }
            ]),
            labelFormatter: function (value) {
              return yearData[value] + '年'
            },
            textStyle: {
              fontSize: 14
            },
            bottom: 15,
            x: 'center',
            width: '90%'
          },
        ],
        xAxis: [
          {
            show: true,
            z: 100,
            name: '年',
            nameTextStyle: {
              fontSize: 16
            },
            type: 'category',
            boundaryGap: false,
            axisLine: {
              onZero: true,
            },
            data: yearData,
          }
        ],
        yAxis: [
          {
            show: false,
          },
        ],
        series: [
          {
            name: '时间线',
            type: 'line',
            areaStyle: {},
            lineStyle: {
              width: 1
            },
          },
        ]
      }
      this.chartInstance.setOption(initOption, 'dark')
      this.chartInstance.on('datazoom', this.getAxisStartAndEnd)
      this.allData = yearData
      //增加屏幕大小变化监听的事件
      window.addEventListener('resize', this.screenAdapter)
      this.screenAdapter()
    },
    screenAdapter () {
      this.size = (this.$refs.tLine_ref.offsetWidth / 100) * 3.6
      let adaptOption = {
        dataZoom: [
          {
            textStyle: {
              fontSize: this.size / 4
            }
          }
        ],
      }
      this.chartInstance.setOption(adaptOption)
      this.chartInstance.resize()
    },
    getAxisStartAndEnd (params) {
      // x轴左端值
      const startYear = this.allData[this.chartInstance.getModel().option.dataZoom[0].startValue]
      //x 轴右端值
      const endYear = this.allData[this.chartInstance.getModel().option.dataZoom[0].endValue]
      this.$emit('getBothEndsVal', {
        startYear: startYear,
        endYear: endYear
      })
    }
  },
}
</script>

<style scoped>
.com-container {
  position: relative;
  margin: auto;
  width: 85%;
}
</style>
